package com.example.demo.service.impl;

import com.example.demo.entity.Safetyinspectionpoweroffarea;
import com.example.demo.handle.PowerOffAreaListParseHandler;
import com.example.demo.mapper.SafetyinspectionpoweroffareaMapper;
import com.example.demo.service.SafetyinspectionpoweroffareaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hys
 * @since 2021-07-16
 */
@Service
public class SafetyinspectionpoweroffareaServiceImpl extends ServiceImpl<SafetyinspectionpoweroffareaMapper, Safetyinspectionpoweroffarea> implements SafetyinspectionpoweroffareaService {

    @Autowired
    private SafetyinspectionpoweroffareaMapper safetyinspectionpoweroffareaMapper;

    @Override
    public Integer insertPowerOffAreaData(String powerOffAreaXml) {
        //1.获取一个SAXParserFactory的实例对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.通过factory的newSAXParser()方法获取一个SAXParser类的对象。
        try {
            SAXParser parser = null;
            try {
                parser = factory.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
            //创建SAXParserHandler对象
            PowerOffAreaListParseHandler powerOffAreaListParseHandler = new PowerOffAreaListParseHandler();
            try {
                // 读取xml文件
                parser.parse(powerOffAreaXml, powerOffAreaListParseHandler);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("~~~~~共有" + powerOffAreaListParseHandler.getSafetyinspectionpoweroffareaArrayList().size() + "个节点被存储");
            for (Safetyinspectionpoweroffarea safetyinspectionpoweroffarea : powerOffAreaListParseHandler.getSafetyinspectionpoweroffareaArrayList()) {
                System.out.println(safetyinspectionpoweroffarea.getCsMineCode());
                System.out.println(safetyinspectionpoweroffarea.getCsDataTime());
                System.out.println(safetyinspectionpoweroffarea.getSsPoffareaCode());
                System.out.println(safetyinspectionpoweroffarea.getSsPoffareaName());
                System.out.println(safetyinspectionpoweroffarea.getSsGisnodeCode());
                System.out.println("----finish----");

                safetyinspectionpoweroffareaMapper.insertPowerOffAreaData(safetyinspectionpoweroffarea.getCsMineCode(),safetyinspectionpoweroffarea.getCsDataTime(),safetyinspectionpoweroffarea.getSsPoffareaCode(),safetyinspectionpoweroffarea.getSsPoffareaName(),safetyinspectionpoweroffarea.getSsGisnodeCode());
            }
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
